faApp.controller('AdListCtrl',['$scope','adService','$location','$controller','$route',function($scope,adService,$location,$controller,$route) {   
	$controller('BaseCtrl', {$scope: $scope}); 
 
  $scope.ads = [];
  $scope.loading = true;

  $scope.$watch('config',function(){
    adService.adList($scope.config).then(function(json){
      $scope.ads = json.list;
      $scope.pager = json.pager;
      $scope.loading = false;
    });
  },true)
  $scope.config = {
    "pageSize" : 20,
    "pageNo" : 1
  };

  $scope.searchMap = {};
  $scope.search = function(notsave){
    angular.forEach($scope.searchMap,function(o,i){
      $scope.config[i] = ( undefined == $scope.searchMap[i] ? "" : $scope.searchMap[i]);
    });
    $scope.config.pageNo = 1;
  };

  $scope.pageChanged = function(){
    $scope.config.pageNo = $scope.pager.pageNo;
  };

  $scope.start = function(id){
    adService.adStart({id:id,status:"01"}).then(function(json){
      $route.reload();    
    });
  }; 

  $scope.stop = function(id){
    adService.adStop({id:id,status:"02"}).then(function(json){
      $route.reload();    
    });
  }; 
}]);

faApp.controller('AdAddCtrl',['$scope','adService','$location','$controller','$timeout',function($scope,adService,$location,$controller,$timeout) {   
	$controller('BaseCtrl', {$scope: $scope}); 

  $scope.ad = {"advertSort":10};
  $scope.loading = true; 

  $scope.save = function(){
    if ($scope.form.$invalid){
      if ($scope.form.title.$invalid)$scope.form.title.$dirty = true;
      if ($scope.form.pic.$invalid)$scope.form.pic.$dirty = true;
      if ($scope.form.link.$invalid)$scope.form.link.$dirty = true;
      if ($scope.form.startTime.$invalid)$scope.form.startTime.$dirty = true;
      if ($scope.form.endTime.$invalid)$scope.form.endTime.$dirty = true;
      if ($scope.form.sort.$invalid)$scope.form.sort.$dirty = true;
    }else{
      if ($scope.ad.startTime){$scope.ad.startTime = $scope.ad.startTime + " 00:00:00";}
      if ($scope.ad.endTime){$scope.ad.endTime = $scope.ad.endTime + " 23:59:59";}

      $scope.ad.linkAddre = $scope.ad.linkAddre;
      $scope.ad.picAddre = $scope.ad.picAddre;
      adService.adAdd($scope.ad).then(function(json){
        if (json.statusCode){
          alert(json.statusCode);
        }else{
          $location.path("/ad/list");
        }
      });
    }
  };

  $scope.onUploadFile({
    showMessage : function(m){ alert(m); },
    onComplete:function(f,response){
      console.info(response)
      if (response.statusCode == "0000") {
        $scope.ad.picAddre = App.staticHost + response.attribute.fileUrl;
        $scope.$apply();
      } else { 
        alert('上传失败'); 
      }
    }    
  });
}]);

faApp.controller('AdEditCtrl',['$scope','adService','$location','$controller','$routeParams','$filter',function($scope,adService,$location,$controller,$routeParams,$filter) {   
	$controller('BaseCtrl', {$scope: $scope}); 

  $scope.ad = {};
  $scope.loading = true; 
  $scope.id = $routeParams.id;

  adService.adQuery($scope.id).then(function(json){
    $scope.ad = json.dto;
    if ($scope.ad.startTime){ $scope.ad.startTime = $filter("date")($scope.ad.startTime,"yyyy-MM-dd"); }
    if ($scope.ad.endTime){ $scope.ad.endTime = $filter("date")($scope.ad.endTime,"yyyy-MM-dd"); }

  });

  $scope.save = function(){
    if ($scope.form.$invalid){
      if ($scope.form.title.$invalid)$scope.form.title.$dirty = true;
      if ($scope.form.pic.$invalid)$scope.form.pic.$dirty = true;
      if ($scope.form.link.$invalid)$scope.form.link.$dirty = true;
      if ($scope.form.startTime.$invalid)$scope.form.startTime.$dirty = true;
      if ($scope.form.endTime.$invalid)$scope.form.endTime.$dirty = true;
      if ($scope.form.sort.$invalid)$scope.form.sort.$dirty = true;
    }else{
      if ($scope.ad.startTime){$scope.ad.startTime = $scope.ad.startTime + " 00:00:00";}
      if ($scope.ad.endTime){$scope.ad.endTime = $scope.ad.endTime + " 23:59:59";}

      $scope.ad.linkAddre = $scope.ad.linkAddre;
      $scope.ad.picAddre = $scope.ad.picAddre;
      adService.adEdit($scope.ad).then(function(json){
        if (json.statusCode){
          alert(json.statusCode);
        }else{
          $location.path("/ad/list");
        }
      });
    }
  };

  $scope.onUploadFile({
    showMessage : function(m){ alert(m); },
    onComplete:function(f,response){
      if (response.statusCode == "0000") {
        $scope.ad.picAddre = App.staticHost + response.attribute.fileUrl;
        $scope.$apply();
      } else { 
        alert('上传失败'); 
      }
    }    
  });
}]);

faApp.controller('MsgListCtrl',['$scope','adService','$location','$controller','$routeParams','$filter','$route', function($scope,adService,$location,$controller,$routeParams,$filter,$route) {
  $controller('BaseListCtrl', {$scope: $scope});

  $scope.msgs = [];
  $scope.loading = true;
  $scope.t=$routeParams.t;
  
  $scope.$watch('config',function(){
    adService.msgList($scope.config).then(function(json){
      $scope.msgs = json.list;
      $scope.pager = json.pager;
      $scope.loading = false;
    });
  },true);

  $scope.config = {
    "pageSize" : 20,
    "pageNo" : 1,
    //早报类型
    "type" : ($scope.t||"")
  };

  $scope.delete = function(id){
    adService.msgDelete(id).then(function(json){
      if (json.statusCode){
        alert("出错了:" + json.statusCode);
      }else{
        $scope.alertNextPage("删除成功！");
        $route.reload();
      }
    });
  };
}]);

faApp.controller('MsgAddCtrl',['$scope','adService','$location','$controller','$routeParams','$filter', function($scope,adService,$location,$controller,$routeParams,$filter) {
  $controller('BaseCtrl', {$scope: $scope});

  $scope.msg = {};
  $scope.addOrUpdate = "添加资讯";

  $scope.t=$routeParams.t;
  if($scope.t){
     //初始化早报信息
    $scope.addOrUpdate="添加早报";
    //正文 初始化富文本
    $scope.msg.contents="<h3 style='margin: 10px 0;text-align: center'> 【资讯快览】 </h3> <p style='color: rgb(255, 0, 0); margin-top: 10px; text-align: left;'> 1、 百度、啊里、腾讯、京东、等耗资780亿入股 </p> <p style='text-align: left;'> 百度、啊里、腾讯、京东、等耗资780亿入股第三方离开过号开始 </p> <h3 style='text-align: center;'> <span style='text-align: center;'>&nbsp;【国际资讯】</span> </h3> <p style='color: rgb(255, 0, 0); margin-top: 10px; text-align: left;'> 1、 百度、啊里、腾讯、京东、等耗资780亿入股 </p> <p style='text-align: left;'> 百度、啊里、腾讯、京东、等耗资780亿入股第三方离开过号开始 </p>";
    $scope.msg.topContent="<p style='color: #ff0000;margin-top: 10px'> 1、百度、啊里、腾讯、京东、等耗资780亿入股 </p> <p> 百度、啊里、腾讯、京东、等耗资780亿入股第三方离开过号开始 </p> <p style='color: rgb(255, 0, 0); margin-top: 10px;'> <br/> </p>";
    //早报类型
    $scope.msg.type="10";
    $scope.msg.sort="2";
  }

  $scope.save = function(){
    if(!$scope.form.$invalid){
      adService.msgAdd($scope.msg).then(function(json){
        if (json.statusCode){
          alert(json.statusCode);
        }else{
          $location.path("/ad/msg-list");
        }
      });
    }else{ 
      if(!$scope.msg.contents){
        alert("请填写正文！");
        return;
      }
    }
  };

  $scope.onUploadFile({
    showMessage : function(m){ alert(m); },
    onComplete:function(f,response){
      if (response.statusCode == "0000") {
        $scope.msg.filePath = App.staticHost + response.attribute.fileUrl;
        $scope.$apply();
      } else { 
        alert('上传失败'); 
      }
    }    
  });

}]);

faApp.controller('MsgUpdateCtrl',['$scope','adService','$location','$controller','$routeParams','$filter', function($scope,adService,$location,$controller,$routeParams,$filter) {
  $controller('BaseCtrl', {$scope: $scope});

  $scope.msg = {};
  $scope.id = $routeParams.id;
  $scope.addOrUpdate = "修改资讯";
  $scope.t=$routeParams.t;
  if($scope.t){
    $scope.addOrUpdate="修改早报";
  }
  if($scope.id){
    adService.msgDetail($scope.id).then(function(json){
      $scope.msg = json.dto;
    });
  }

  $scope.save = function(){
    if(!$scope.form.$invalid){
      adService.msgUpdate($scope.msg).then(function(json){
        if (json.statusCode){
          alert(json.statusCode);
        }else{
          $location.path("/ad/msg-list");
        }
      });
    }else{
      if(!$scope.msg.contents){
        alert("请填写正文！");
        return;
      }
    }
  };

  $scope.onUploadFile({
    showMessage : function(m){ alert(m); },
    onComplete:function(f,response){
      if (response.statusCode == "0000") {
        $scope.msg.filePath = App.staticHost + response.attribute.fileUrl;
        $scope.$apply();
      } else { 
        alert('上传失败'); 
      }
    }    
  });

}]);

faApp.controller('ActListCtrl',['$scope','adService','$location','$controller','$routeParams','$filter', function($scope,adService,$location,$controller,$routeParams,$filter) {
  $controller('BaseListCtrl', {$scope: $scope});

  $scope.acts = [];
  $scope.loading = true;
  $scope.$watch('config',function(){
    adService.actList($scope.config).then(function(json){
      $scope.acts = json.list;
      $scope.pager = json.pager;
      $scope.loading = false;
    });
  },true);

  $scope.config = {
    "pageSize" : 20,
    "pageNo" : 1,
    "publicActivitiesId":0,
  };
         //点击事件显示消息
  $scope.changeShow=function(id){
    $scope.config.publicActivitiesId=id;
    adService.actPerList($scope.config).then(function(json){
     
      $scope.actPres = json.publicActivitiesPersonList;

      $scope.loading = false;
    });
      $scope.isShowOrder = true;
  }
}]);

faApp.controller('ActAddCtrl',['$scope','adService','$location','$controller','$routeParams','$filter', function($scope,adService,$location,$controller,$routeParams,$filter) {
  $controller('BaseCtrl', {$scope: $scope});

  $scope.act = {};
   // 对Date的扩展，将 Date 转化为指定格式的String   
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，   
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)   
// 例子：   
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423   
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18   
$scope.dateFormat = function(dataValue)   
{ //author: meizz   


  var fmt = "yyyy-MM-dd hh:mm:ss";
  var o = {   
    "M+" : dataValue.getMonth()+1,                 //月份   
    "d+" : dataValue.getDate(),                    //日   
    "h+" : dataValue.getHours(),                   //小时   
    "m+" : dataValue.getMinutes(),                 //分   
    "s+" : dataValue.getSeconds(),                 //秒   
    "q+" : Math.floor((dataValue.getMonth()+3)/3), //季度   
    "S"  : dataValue.getMilliseconds()             //毫秒   
  };   
  if(/(y+)/.test(fmt))   
    fmt=fmt.replace(RegExp.$1, (dataValue.getFullYear()+"").substr(4 - RegExp.$1.length));   
  for(var k in o)   
    if(new RegExp("("+ k +")").test(fmt))   
  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
  return fmt;   
} 

  $scope.save = function(){
    if(!$scope.form.$invalid){
      if($scope.act){
        if($scope.act.activitiesTotalPerson <= 0){
          alert("报名人数要大于0！");
          return;
        }
        if($scope.act.activitiesStartTime > $scope.act.activitiesEndTime){
          alert("结束时间要大于开始时间！");
          return;
        }
        if($scope.act.activitiesRegistEndTime > $scope.act.activitiesEndTime){
          alert("结束时间要大于报名结束时间！");
          return;
        }
      }

      
      $scope.act.activitiesStartTime = $filter("date")($scope.act.activitiesStartTime,"yyyy-MM-dd HH:mm:ss");
      $scope.act.activitiesEndTime = $filter("date")($scope.act.activitiesEndTime,"yyyy-MM-dd HH:mm:ss");
      $scope.act.activitiesRegistEndTime =  $filter("date")($scope.act.activitiesRegistEndTime,"yyyy-MM-dd HH:mm:ss");

      adService.actAdd($scope.act).then(function(json){
        if (json.statusCode){
          alert(json.statusCode);
        }else{
          $location.path("/ad/act-list");
        }
      });
    }else{
      if(!$scope.act.activitiesRecomment){
        alert("请填写具体内容！");
        return;
      }
    }
  };

  $scope.onUploadFile({
    showMessage : function(m){ alert(m); },
    onComplete:function(f,response){
      if (response.statusCode == "0000") {
        // $scope.act.activitiesImageUri = App.staticHost + response.attribute.fileUrl;
        $scope.act.activitiesImageUri = response.attribute.fileUrl;
        $scope.$apply();
      } else { 
        alert('上传失败'); 
      }
    }    
  });

}]);

faApp.controller('ActUpdateCtrl',['$scope','adService','$location','$controller','$routeParams','$filter', function($scope,adService,$location,$controller,$routeParams,$filter) {
  $controller('BaseCtrl', {$scope: $scope});

  $scope.act = {};
  $scope.id = $routeParams.id;

  if($scope.id){
      adService.actDetail($scope.id).then(function(json){
      $scope.act = json.dto;
      $scope.act.activitiesImageUri = $scope.act.activitiesImageUri;
      $scope.act.activitiesStartTime = $filter("date")(json.dto.activitiesStartTime,"yyyy-MM-ddTHH:mm");
      $scope.act.activitiesEndTime = $filter("date")(json.dto.activitiesEndTime,"yyyy-MM-ddTHH:mm");
      $scope.act.activitiesRegistEndTime = $filter("date")(json.dto.activitiesRegistEndTime,"yyyy-MM-ddTHH:mm");
      delete $scope.act.activitiesPublishTime;
    });
  }

  $scope.save = function(){
    if(!$scope.form.$invalid){

      $scope.act.activitiesStartTime = $filter("date")($scope.act.activitiesStartTime,"yyyy-MM-dd HH:mm:ss");
      $scope.act.activitiesEndTime = $filter("date")($scope.act.activitiesEndTime,"yyyy-MM-dd HH:mm:ss");
      $scope.act.activitiesRegistEndTime =  $filter("date")($scope.act.activitiesRegistEndTime,"yyyy-MM-dd HH:mm:ss");
      adService.actUpdate($scope.act).then(function(json){
        if (json.statusCode){
          alert(json.statusCode);
        }else{
          $location.path("/ad/act-list");
        }
      });
    }else{
      if(!$scope.act.activitiesRecomment){
        alert("请填写具体内容！");
        return;
      }
    }
  };

  $scope.onUploadFile({
    showMessage : function(m){ alert(m); },
    onComplete:function(f,response){
      if (response.statusCode == "0000") {
        // $scope.act.activitiesImageUri = App.staticHost + response.attribute.fileUrl;
        $scope.act.activitiesImageUri = response.attribute.fileUrl;
        $scope.$apply();
      } else { 
        alert('上传失败'); 
      }
    }    
  });

}]);

faApp.controller('AdSuperList',['$scope','productService','$location','$controller','$route','adService','$routeParams',function($scope,productService,$location,$controller,$route,adService, $routeParams) {   
  $controller('BaseListCtrl', {$scope: $scope});

  $scope.recommendList = [];

  $scope.$watch('config',function(){
    adService.adRecommendList($scope.config).then(function(json){
      $scope.recommendList = json.list;
      // $scope.pager = json.pager;
      // $scope.loading = false;
    });
  },true);

  $scope.config = {
    "pageSize" : 100,
    "pageNo" : 1,
    "advertPosition" : '14'
  };

  $scope.toAddRecommend = function(){
    if($scope.recommendList && $scope.recommendList.length >= 5){
      alert("最多能有五项！");
      return;
    }else{
      $location.path("/super/add").search({t : $scope.config.advertPosition || '14'});
    }
  }

  $scope.start = function(id){
    adService.adStart({id:id,status:"01"}).then(function(json){
      $route.reload();    
    });
  }; 

  $scope.stop = function(id){
    var t = 0;
    angular.forEach($scope.recommendList,function(o){
      if(o.status == '01')
        t++;
    });
    if(t <= 2){
      alert("最少得有两项！");
      return;
    }
    adService.adStop({id:id,status:"02"}).then(function(json){
      $route.reload();    
    });
  };

}]);

faApp.controller('AdSuperAdd', ['$scope', 'productService', '$location', '$controller', '$route', 'adService', '$routeParams', function($scope, productService, $location, $controller, $route, adService, $routeParams) {
  $controller('BaseListCtrl', { $scope: $scope });

  $scope.recommend = {
    title:'',
    pic:'',
    linkAddre:'',
    startTime:'1990-01-01 00:00:00',
    endTime:'3000-01-01 23:59:59',
    status:'01',
    advertSort:'1',
    picAddre:'',
    advertPosition: $routeParams.t || '10'
  };

  $scope.save = function(){
    if (!$scope.form.$invalid){
      if(!$scope.recommend.picAddre && $scope.recommend.advertPosition != '14'){
        alert("请上传图片!");
        return;
      }
      adService.adAdd($scope.recommend).then(function(json){
        if (json.statusCode){
          alert(json.statusCode);
        }else{
          $location.path("/super/list").search({t : $scope.recommend.advertPosition});
        }
      });
    }else{
      alert("请把信息填写完整！");
    }
  }

  $scope.onUploadFile({
    showMessage: function(m) { alert(m); },
    onComplete: function(f, response, id) {
      if (response.statusCode == "0000") {
        $scope.recommend.picAddre = response.attribute.fileUrl;
        $scope.$apply();
      } else {
        alert('上传失败');
      }
    }
  });

}]);

faApp.controller('AdSuperEdit', ['$scope', 'productService', '$location', '$controller', '$route', 'adService', '$routeParams','$filter' , function($scope, productService, $location, $controller, $route, adService, $routeParams, $filter) {
  $controller('BaseListCtrl', { $scope: $scope });

  $scope.recommend = {};
  $scope.id = $routeParams.id;

  if($scope.id){
    adService.adQuery($scope.id).then(function(json){
      $scope.recommend = json.dto;

      $scope.recommend.startTime = $filter("date")($scope.recommend.startTime,"yyyy-MM-dd") + " 00:00:00";;
      $scope.recommend.endTime = $filter("date")($scope.recommend.endTime,"yyyy-MM-dd") + " 23:59:59";
    });
  }

  $scope.save = function(){
    if (!$scope.form.$invalid){
      if(!$scope.recommend.picAddre && $scope.recommend.advertPosition != '14'){
        alert("请上传图片!");
        return;
      }
      adService.adEdit($scope.recommend).then(function(json){
        if (json.statusCode){
          alert(json.statusCode);
        }else{
          $location.path("/super/list").search({t : $scope.recommend.advertPosition});
        }
      });
    }else{
      alert("请把信息填写完整！");
    }
  }

  $scope.onUploadFile({
    showMessage: function(m) { alert(m); },
    onComplete: function(f, response, id) {
      if (response.statusCode == "0000") {
        $scope.recommend.picAddre = response.attribute.fileUrl;
        $scope.$apply();
      } else {
        alert('上传失败');
      }
    }
  });
}]);